package org.huangxiaozhe.adventure;

import org.huangxiaozhe.adventure.common.ConstantUtil;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;


public class Bullet {
	
	float x;
	float y;
	float sin;
	float cos;
	float span = 6f;
	boolean run = false;
	public static final int maxTan = 2;		//y/x
	public static final int minTan = -2;	//y/x
	Paint paint  = new Paint();
	
	public Bullet(float startX, float startY, float toX, float toY){
		this.x = startX;
		this.y = startY;
		float len = (float) Math.sqrt(Math.pow(toX - startX, 2) + Math.pow(toY - startY, 2));
		sin = (toY - startY)/len;
		cos = (toX - startX)/len;
		run = true;
	}

	public void draw(Canvas canvas){
		if(run){
			paint.setColor(Color.YELLOW);
			canvas.drawCircle(x, y, 5, paint);
			paint.setColor(Color.RED);
			canvas.drawCircle(x, y, 3, paint);
		}
	}
	
	public boolean getEnable(){
		float tan = sin / cos;
		return tan <= maxTan && tan >= minTan;
	}
	
	public boolean Move(){
		if(run){
			float xt = x + span * cos;
			float yt = y + span * sin;
			if(xt >= ConstantUtil.screenWidth || xt <= 0){
				run = false;
				return run;
			}
			if(yt >= ConstantUtil.screenHeight || yt <= 0){
				run = false;
				return run;
			}
			x = xt;
			y = yt;
		}
		return run;
	}
	
}
